package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Transactional
@RestController
@RequestMapping("/category")
@Slf4j

public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired

    private DishService dishService;

    @Autowired

    private SetmealService setmealService;

    /**
     * 录入新增菜品
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){

        log.info("本次需要修改的菜品为：{}",category.getName());

        categoryService.save(category);

        return R.success("录入成功");
    }

    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize){

        log.info("page:{},pagesizge:{}",page,pageSize);

        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.orderByAsc(Category::getSort);

        categoryService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){

        log.info("需要修改的菜品为：{}",category.toString());

        categoryService.updateById(category);

        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List> list(Category category){

        log.info("需要修改的菜品为：{}",category.toString());

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType())
                .orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);

        return  R.success(list);
    }

    /**
     * 根据ID删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){

        log.info("本次请求删除的ID为：{}",id);

        //categoryService.remove(id);

        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<Setmeal>();

        setmealQueryWrapper.eq(Setmeal::getCategoryId,id);

        int count1 = setmealService.count(setmealQueryWrapper);

        if (count1>0) {
            throw new CustomException("已经关联了套餐 不能删除");
        }

        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<Dish>();

        dishQueryWrapper.eq(Dish::getCategoryId,id);

        int count2 = dishService.count(dishQueryWrapper);

        if (count2>0) {
            throw new CustomException("已经关联了菜品 不能删除");
        }

        categoryService.removeById(id);

        return R.success("没有关联套餐和菜品 可以删除");

    }
}
